package org.skyfree.ghyll.tcard.repository.action;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.graphics.Image;
import org.skyfree.ghyll.tcard.core.ITRepository;
import org.skyfree.ghyll.tcard.repository.wizard.NewCategoryDialog;

public class AddCategoryMenuItemAction extends AbstractMenuItemAction {

	public AddCategoryMenuItemAction(int style, String text, int Accelerator,
			Image image,TreeViewer treeViewer) 
	{
		super(style, text, Accelerator, image,treeViewer);
	}

	@Override
	public void execute(Object source) throws Exception 
	{
		ITRepository repo = (ITRepository)item.getData();
		NewCategoryDialog inputName = new NewCategoryDialog(this.treeViewer.getTree().getShell(),repo);
		if(inputName.open()==IDialogConstants.OK_ID)
		{
			repo.createCategory(inputName.getCategoryName());
			treeViewer.refresh();
		}
	}

	@Override
	public boolean isEnable() 
	{
		Object data = this.item.getData();
		if(data instanceof ITRepository)
		{
			return true;
		}
		return false;
	}
	
}
